<?php
/**
 * Created by PhpStorm.
 * User: liujialiang
 * Date: 2017/9/22
 * Time: 10:18
 */

namespace App\Controller;


use App\Model\FriendsModel;
use App\Model\UserModel;
use lib\View;

class FriendsController
{
//    public function index(){
//        $user=new UserModel();
//        $userData=$user->get();
//        foreach($userData as $k=>$v){
//            if($v['id']==$_SESSION['sns_user']['id']){
//                unset($userData[$k]);
//            }
//        }
//        return new View('user.addfriends',['userData'=>$userData]);
//    }
//
//    public function addFriends(){
//        p(1);
//        $fid=$_GET['fid'];
//        $f=new FriendsModel();
//        $data['uid']=$_SESSION['sns_user']['id'];
//        $data['fid']=$fid;
//        $res=$f->save($data);
//        if($res){
//            success('添加好友成功!','addFriends');
//        }else{
//            error('添加好友失败!');
//        }
//    }



    public function index(){
        //去掉已经加为好有的用户信息
        //1.获取已经加为好友的用户id
        $fids=[];
        $f=new FriendsModel();
        $frData=$f->get(['uid'=>$_SESSION['sns_user']['id']]);
        foreach($frData as $v){
            $fids[]=$v['fid'];
        }
        $user=new UserModel();
        $userData=$user->get();
        //去掉登录用户的信息
        foreach($userData as $k=>$v){
            if($v['id']==$_SESSION['sns_user']['id']){
                unset($userData[$k]);
            }
            //去掉好友信息
            if(in_array($v['id'],$fids)){
                unset($userData[$k]);
            }
        }

        //返回好友信息
        $fData=$user->join(['friends'=>'fid'],['friends.uid'=>$_SESSION['sns_user']['id']]);


        return new View('user.addfriends',['userData'=>$userData,'fData'=>$fData]);
    }

    public function addFriends(){
        $fid=$_GET['fid'];
        $f=new FriendsModel();
        $data['uid']=$_SESSION['sns_user']['id'];
        $data['fid']=$fid;
        $res=$f->save($data);
        if($res){
            success('添加好友成功!','addFriends');
        }else{
            error('添加好友失败!');
        }
    }



}